package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateCustomProcessBaseUserRel
//@description: 创建CustomProcessBaseUserRel记录
//@param: customProcessBaseUserRel model.CustomProcessBaseUserRel
//@return: err error

func CreateCustomProcessBaseUserRel(customProcessBaseUserRel model.CustomProcessBaseUserRel) (err error) {
	err = global.GVA_DB.Create(&customProcessBaseUserRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseUserRel
//@description: 删除CustomProcessBaseUserRel记录
//@param: customProcessBaseUserRel model.CustomProcessBaseUserRel
//@return: err error

func DeleteCustomProcessBaseUserRel(customProcessBaseUserRel model.CustomProcessBaseUserRel) (err error) {
	err = global.GVA_DB.Delete(customProcessBaseUserRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteCustomProcessBaseUserRelByIds
//@description: 批量删除CustomProcessBaseUserRel记录
//@param: ids request.IdsReq
//@return: err error

func DeleteCustomProcessBaseUserRelByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.CustomProcessBaseUserRel{},"id in ?",ids.Ids).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: UpdateCustomProcessBaseUserRel
//@description: 更新CustomProcessBaseUserRel记录
//@param: customProcessBaseUserRel *model.CustomProcessBaseUserRel
//@return: err error

func UpdateCustomProcessBaseUserRel(customProcessBaseUserRel model.CustomProcessBaseUserRel) (err error) {
	err = global.GVA_DB.Save(&customProcessBaseUserRel).Error
	return err
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseUserRel
//@description: 根据id获取CustomProcessBaseUserRel记录
//@param: id uint
//@return: err error, customProcessBaseUserRel model.CustomProcessBaseUserRel

func GetCustomProcessBaseUserRel(id uint) (err error, customProcessBaseUserRel model.CustomProcessBaseUserRel) {
	err = global.GVA_DB.Where("id = ?", id).First(&customProcessBaseUserRel).Error
	return
}

//@author: [piexlmax](https://github.com/piexlmax)
//@function: GetCustomProcessBaseUserRelInfoList
//@description: 分页获取CustomProcessBaseUserRel记录
//@param: info request.CustomProcessBaseUserRelSearch
//@return: err error, list interface{}, total int64

func GetCustomProcessBaseUserRelInfoList(info request.CustomProcessBaseUserRelSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&model.CustomProcessBaseUserRel{})
    var customProcessBaseUserRels []model.CustomProcessBaseUserRel
    // 如果有条件搜索 下方会自动创建搜索语句
    if info.BaseId != 0 {
        db = db.Where("`base_id` = ?",info.BaseId)
    }
    if info.UserId != 0 {
        db = db.Where("`user_id` = ?",info.UserId)
    }
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&customProcessBaseUserRels).Error
	return err, customProcessBaseUserRels, total
}